package com.vue3.blog.web;

import cn.hutool.core.collection.CollUtil;
import com.vue3.blog.common.resp.PageVO;
import com.vue3.blog.common.resp.Result;
import com.vue3.blog.domain.dto.ArticleDTO;
import com.vue3.blog.domain.dto.ArticleQueryDTO;
import com.vue3.blog.domain.vo.ArticleDetailVO;
import com.vue3.blog.service.IArticleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author Mr.小朱同学
 * @version 1.0.0
 * @date 2025/6/9 19:13
 * @description 博客文章
 */
@RestController
@RequestMapping("/api/article")
public class ArticleController {
    @Resource
    private IArticleService articleService;


    @PostMapping("/save")
    public Result<Void> save(@RequestBody @Valid ArticleDTO dto) {
        articleService.saveArticle(dto);
        return Result.success("保存成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        articleService.removeArticle(id);
        return Result.success("删除文章");
    }

    @GetMapping("/detail/{id}")
    public Result<ArticleDetailVO> detail(@PathVariable Long id) {
        return Result.success("文章详情", articleService.getArticleDetail(id));
    }

    @GetMapping("/list")
    public Result<PageVO<ArticleDetailVO>> list(ArticleQueryDTO dto) {
        return Result.success("文章列表", articleService.getArticleList(dto));
    }

    @GetMapping("/frontend/list")
    public Result<PageVO<ArticleDetailVO>> frontendList(ArticleQueryDTO dto) {
        // 只查询已发布的文章
        dto.setStatusList(CollUtil.toList(1));
        return Result.success("文章列表", articleService.getArticleList(dto));
    }

    @GetMapping("/frontend/new-list")
    public Result<List<ArticleDetailVO>> newArticleList() {
        return Result.success("最新文章", articleService.getNewArticleList());
    }

    @GetMapping("/frontend/prev-next")
    public Result<?> getPrevNextArticle(@RequestParam Long id) {
        return Result.success("上一篇、下一篇文章", articleService.getPrevNextArticle(id));
    }
}